using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using SIMSProject.Helpers.QuickLaunch;
using SIMSProject.Timer_Job;

namespace SIMSProject.Features.Feature1
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("933ae29b-741c-4c43-a741-e2a652dec4c0")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        const string List_JOB_NAME = "[SIMS]Order Notification Timer Job";

        #region
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;

            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (string.Equals(job.Name, List_JOB_NAME))
                {
                    job.Delete();
                }
            }

            OrderNotificationTimerJob timerJob = new OrderNotificationTimerJob(List_JOB_NAME, site.WebApplication);

            SPDailySchedule schedule = new SPDailySchedule();
	        schedule.BeginSecond = 0;
	        schedule.EndSecond = 59;
            timerJob.Schedule = schedule;
            timerJob.Update();
            //this.AddQuickLaunchManager(web);
        }
        #endregion

        #region Feature Deactivating
        /// <summary>
        /// Override method FeatureDeactivating
        /// </summary>
        /// <param name="properties"> feature's properties </param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;

            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (string.Equals(job.Name, List_JOB_NAME))
                {
                    job.Delete();
                }
            }

            SPWeb currentWeb = properties.Feature.Parent as SPWeb;
            if (currentWeb != null)
            {
                // DeactivateFeature(currentWeb, "Questionnaire", "DeutscheBank.SharePoint.LNAR.ADA0347ExitQuestionnaire.Event_Handler.ItemPermissionEventHandler");
                // DeactivateFeature(currentWeb, "Archived of Questionnaire", "DeutscheBank.SharePoint.LNAR.ADA0347ExitQuestionnaire.Event_Handler.ArchivedListEventHandler");
            }
        }

        private static void DeactivateFeature(SPWeb currentWeb, string listName, string className)
        {
            try
            {
                SPEventReceiverDefinitionCollection allEventsOfCurrentList = currentWeb.Lists[listName].EventReceivers;
                int count = allEventsOfCurrentList.Count;
                for (int i = 0; i < count; i++)
                {
                    if (allEventsOfCurrentList[i].Class == className && (allEventsOfCurrentList[i].Type == SPEventReceiverType.ItemAdded || allEventsOfCurrentList[i].Type == SPEventReceiverType.ItemUpdated))
                    {
                        allEventsOfCurrentList[i].Delete();
                        i--;
                    }

                    count = allEventsOfCurrentList.Count;
                }
            }
            catch (Exception ex)
            {

            }
        }

        #endregion

        #region
        private void AddQuickLaunchManager(SPWeb web)
        {
            QuickLaunchManager.CreateQuickLaunchDataSource(web);

            // Add heading
            var titleAdministratorId = QuickLaunchManager.AddHeading(web, new QuickLaunchItem("Administration") { ListId = web.Lists["Roles"].ID });
        }
        #endregion
    }
}
